########################################################################################################################
#
# ls(1) variants   resembling Ubuntu's default config.
#
# Common features:
#	- Colorize output
#	- Show directories first
#
#	- l  Hide dotfiles  and  backup files (foo~)
#	  ls Hide dotfiles, show backup files
#	  la Show dotfiles  and  backup files
#	  ll Show dotfiles  and  backup files
#	  (In Ubuntu, only la shows dotfiles, and none of these hides backup files.  Ubuntu l is kind of useless for interactive session.)
#
# ll-specific features:
#	- Show override reminder “Running ls -lAh color dirfirst mytime $@”  (helpful when copying interactive session into a script)
#	- More spacing between columns, less spacing within mtime column.
#	- Human-readable file size ("153K" instead of "155992")
#	- Hide leading zeroes in time (" 8: 3" instead of "08:03")
#	- (l10n) Right-align date
#	  (l10n) Show "日"
#	  (l10n) "05年" instead of "2005"
#	- (l10n) Gray out some parts of date to improve skimmability
#
# ll-specific reminders:
#	- Hardcoded color control sequence
#	- (l10n) Hardcoded Chinese text. Ignores env vars (LC_TIME, etc)
#
########################################################################################################################





##
## compatibility: tested in Dash 0.5.10 (Linux)
##                          Bash 5.1    (Linux)
##                          Zsh  5.8    (Linux)
## external deps: ls                                          (false(1) is actually built-in in dash)
##                cat (for testing)
##





alias ls='ls --color=auto'
alias  l='ls -B --group-directories-first'
alias la='ls -A --group-directories-first'


#
# Accepts arguments & env vars. Retains termination status. Reasonable pipeline behavior.
#
# Graying depends on terminal palette
#    (Usually the default palette works fine.  If it doesn't work for you, try
#     setting 8th color to as close to background color as possible, but not so
#     faint that you have to lean forward to see the text clearly.)
#
ll () {
  local t="+\
   %_m月%_d日  %y年  
   %_m月%_d日 %_H:%_M  "

  [ -t 1 ] && t="+\
   %_m[37m月[39m%_d日 [37m %y年[39m  
   %_m[37m月[39m%_d日 [37m%_H:%_M[39m  "

  echo 'Running ls -lAh color dirfirst mytime $@' >&2
  command ls  -lAh  --color=auto  --group-directories-first  --time-style="$t"  "$@"
}




# Test
if false; then
	echo2 () {
		echo $*
		echo; echo
	}
	test_ls_module () {
		ls;     echo2 $?;                ls >/dev/null;     echo2 $?;                ls 2>/dev/null | cat;     echo2 $?
		l ;     echo2 $?;                l  >/dev/null;     echo2 $?;                l  2>/dev/null | cat;     echo2 $?
		la;     echo2 $?;                la >/dev/null;     echo2 $?;                la 2>/dev/null | cat;     echo2 $?
		ll;     echo2 $?;                ll >/dev/null;     echo2 $?;                ll 2>/dev/null | cat;     echo2 $?
	}

	test_ls_module
fi
